﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Krile.Kernel;

namespace Wassrile.Dialogs
{
    public partial class IntelliNantoka : Form
    {
        string[] cand;
        Image img;
        public event EventHandler OnWindowHided;
        public event EventHandler OnDecided;

        public string DecidedItem
        {
            get
            {
                var i = candidates.Items[candidates.SelectedIndex] as string;
                if ( i != null && i.Contains('%'))
                {
                    return i.Split('%')[0];
                }
                else
                {
                    return i;
                }
            }
        }

        private Controls.WInput parentInput;
        private IntelliNantoka(Controls.WInput parent)
        {
            parentInput = parent;
        }
        public IntelliNantoka(Controls.WInput parent, string[] candidates, Image img)
            :this(parent)
        {
            SetStyle(ControlStyles.Selectable, false);
            this.img = img;
            cand = candidates;
            InitializeComponent();
        }
        public IntelliNantoka(Controls.WInput parent, string[] keys, DisplayCallback callback)
            : this(parent)
        {
            SetStyle(ControlStyles.Selectable, false);
            this.img = null;
            this.callback = callback;
            cand = keys;
            InitializeComponent();
        }

        private void IntelliNantoka_Load(object sender, EventArgs e)
        {
            candidates.Items.AddRange(cand);
            candidates.SelectedIndex = 0;
        }

        public void MoveBelowItem()
        {
            if (candidates.SelectedIndex < candidates.Items.Count - 1)
                candidates.SelectedIndex++;
        }

        public void MoveAboveItem()
        {
            if (candidates.SelectedIndex > 0)
                candidates.SelectedIndex--;
        }

        public bool SelectStartsWith(string key)
        {
            if (String.IsNullOrEmpty(key))
            {
                candidates.SelectedIndex = 0;
                return true;
            }
            else
            {
                for (int i = 0; i < candidates.Items.Count; i++)
                {
                    if (candidates.Items[i].ToString().StartsWith(key))
                    {
                        candidates.SelectedIndex = i;
                        return true;
                    }
                }
                return false;
            }
        }

        public delegate Image DisplayCallback(string key, ref string display);

        DisplayCallback callback;

        private void candidates_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            if (e.Index < 0 || e.Index >= candidates.Items.Count) return;
            if (e.Index % 2 == 0 && (e.State & DrawItemState.Selected) != DrawItemState.Selected)
                e.Graphics.FillRectangle(Brushes.WhiteSmoke, e.Bounds);
            var dispString = candidates.Items[e.Index].ToString();
            Rectangle bound = e.Bounds;
            if (img != null)
            {
                e.Graphics.DrawImage(img, e.Bounds.Location);
                bound.X += img.Width;
                bound.Width -= img.Width;
            }
            else
            {
                var ig = callback(candidates.Items[e.Index].ToString(), ref dispString);
                e.Graphics.DrawImage(ig, e.Bounds.Location);
                bound.X += ig.Width;
                bound.Width -= ig.Width;
            }


            TextRenderer.DrawText(
                e.Graphics, dispString.Replace("%",""),
                Bridge.Config.DisplayConfig.RenderingFont, bound, e.ForeColor, 
                 TextFormatFlags.Left | TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter);

            e.DrawFocusRectangle();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
        }

        private void IntelliNantoka_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (OnDecided != null)
                    OnDecided.Invoke(this, new EventArgs());
                this.Visible = false;
            }
            else if (e.KeyCode == Keys.Escape)
            {
                this.Visible = false;
            }
            else
            {
                parentInput.ParentICM.FocusToInput();
            }
        }

        private void IntelliNantoka_VisibleChanged(object sender, EventArgs e)
        {
            if (!this.Visible)
                OnWindowHided.Invoke(this, new EventArgs());
        }

        private void IntelliNantoka_Shown(object sender, EventArgs e)
        {
            parentInput.ParentICM.FocusToInput();
        }

        private void candidates_MouseUp(object sender, MouseEventArgs e)
        {
            parentInput.ParentICM.FocusToInput();
        }

        private void IntelliNantoka_MouseUp(object sender, MouseEventArgs e)
        {
            parentInput.ParentICM.FocusToInput();
        }

        private void candidates_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (OnDecided != null)
                OnDecided.Invoke(this, new EventArgs());
        }
    }
}
